package sg.edu.nus.iss.ooad.jass.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import sg.edu.nus.iss.ooad.jass.dao.IResponseDAO;
import sg.edu.nus.iss.ooad.jass.dao.pattern.AbstractJASSDAOFactory;
import sg.edu.nus.iss.ooad.jass.entity.Response;

/**
 *
 * Response Controller. This class will handle all request related to the Response object.
 *
 * @author Isak Rabin (HT082193M)
 * @version 1.0
 * @since 1.0
 *
 * Development Environment : Netbeans IDE v6.7
 * Name of the File        : ResponseController.java
 * Creation / Modification History
 *         Isak Rabin        20-Sep-2009        Created
 *
 */
public class ResponseController {

        private final static Logger logger = Logger.getLogger(ResponseController.class.getName());

        public ResponseController() {
        }

        /**
         *
         * @param response
         * @return
         * @throws Exception
         */
        public boolean createResponse(Response response) throws Exception {
                IResponseDAO dao = null;
                boolean retValue = false;

                try {
                        dao = AbstractJASSDAOFactory.getJASSDAO().getResponseDAO();
                        retValue = dao.createResponse(response);
                } catch (Exception ex) {
                        logger.log(Level.SEVERE, null, ex);
                        ex.printStackTrace();
                } finally {
                        dao = null;
                }

                return retValue;
        }

        /**
         * 
         * @param id
         * @return
         * @throws Exception
         */
        public Response retrieveResponse(String id) throws Exception {
                IResponseDAO dao = null;
                Response retValue = null;

                try {
                        dao = AbstractJASSDAOFactory.getJASSDAO().getResponseDAO();
                        retValue = dao.retrieveResponse(id);
                } catch (Exception ex) {
                        logger.log(Level.SEVERE, null, ex);
                        ex.printStackTrace();
                } finally {
                        dao = null;
                }

                return retValue;
        }

        /**
         *
         * @param response
         * @return
         * @throws Exception
         */
        public boolean updateResponse(Response response) throws Exception {
                IResponseDAO dao = null;
                boolean retValue = false;

                try {
                        dao = AbstractJASSDAOFactory.getJASSDAO().getResponseDAO();
                        retValue = dao.updateResponse(response);
                } catch (Exception ex) {
                        logger.log(Level.SEVERE, null, ex);
                        ex.printStackTrace();
                        throw ex;
                } finally {
                        dao = null;
                }

                return retValue;
        }

        /**
         * 
         * @param response
         * @return
         * @throws Exception
         */
        public boolean deleteResponse(Response response) throws Exception {
                IResponseDAO dao = null;
                boolean retValue = false;

                try {
                        dao = AbstractJASSDAOFactory.getJASSDAO().getResponseDAO();
                        retValue = dao.deleteResponse(response);
                } catch (Exception ex) {
                        logger.log(Level.SEVERE, null, ex);
                        ex.printStackTrace();
                        throw ex;
                } finally {
                        dao = null;
                }

                return retValue;
        }

        /**
         * 
         * @return
         * @throws Exception
         */
        public List<Response> getAllResponse() throws Exception {

                IResponseDAO dao = null;
                List<Response> retValue = new ArrayList<Response>();

                try {
                        dao = AbstractJASSDAOFactory.getJASSDAO().getResponseDAO();
                        retValue = dao.getAllResponse();
                } catch (Exception ex) {
                        logger.log(Level.SEVERE, null, ex);
                        ex.printStackTrace();
                        throw ex;
                } finally {
                        dao = null;
                }

                return retValue;

        }

        /**
         *
         * @param jobadvertisementID
         * @return
         * @throws Exception
         */
        public List<Response> findResponseByJobAdvertisementID(String jobadvertisementID) throws Exception {

                IResponseDAO dao = null;
                List<Response> retValue = new ArrayList<Response>();
                try {
                        dao = AbstractJASSDAOFactory.getJASSDAO().getResponseDAO();
                        retValue = dao.findResponseByJobAdvertisementID(jobadvertisementID);
                } catch (Exception ex) {
                        logger.log(Level.SEVERE, null, ex);
                        ex.printStackTrace();
                        throw ex;
                } finally {
                        dao = null;
                }

                return retValue;

        }

        /**
         *
         * @param job
         * @return
         */
        public boolean isResponsed(String jobadvertisementID) throws Exception {
                return !findResponseByJobAdvertisementID(jobadvertisementID).isEmpty();
        }

        /**
         * Load Response object from persistent and cache it.
         * @throws java.lang.Exception
         */
        public synchronized static void cache() throws Exception {
                IResponseDAO dao = null;
                try {
                        dao = AbstractJASSDAOFactory.getJASSDAO().getResponseDAO();
                } catch (Exception ex) {
                        logger.log(Level.SEVERE, null, ex);
                        ex.printStackTrace();
                        throw ex;
                } finally {
                        dao = null;
                }
        }
}
